

#include <p16F887.inc>
	__CONFIG    _CONFIG1, _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
	__CONFIG    _CONFIG2, _WRT_OFF & _BOR21V

     cblock     0x20
Delay1               ; Assign an address to label Delay1
Delay2     
Display              ; define a variable to hold the diplay
Display2			 ; display 2 is used for the first bit only for the task 2, taken to set display 1
Direction 
LookingFor
T0Semaphore
     endc
     
; Flag Definitions
     cblock 0x70     ; put these up in unbanked RAM
W_Save
STATUS_Save
     endc
     
     org 0
     goto      Start

     org 4
ISR:   
     movwf     W_Save              ; Save context
     movf      STATUS,w
     movwf     STATUS_Save
     
     btfsc     INTCON,T0IF
     goto      ServiceTimer0

     goto      ExitISR          
     
ServiceTimer0:
     bcf       STATUS,RP0          ; Ensure ISR executes in Register Bank 0
     bcf       STATUS,RP1
     bcf       INTCON,T0IF         ; clear the interrupt flag. (must be done in software)
     bsf       T0Semaphore,0       ; signal the main routine that the Timer has expired
     banksel	Display2
	 ;btfss		Display2,0
	;	goto	TurnOn
	; TurnOff;turn off
	;	bcf		Display2,0
	 ;goto ExitISR
	 ;TurnOn;turn on
	;	bsf		Display2,0
     ;movwf     TMR0                ; Also clears the prescaler
	decfsz	Delay1,f
		goto SkipThis
	;comf	Display2,f
	comf	Display2,f
	SkipThis
     goto      ExitISR
               
ExitISR:
	 banksel INTCON
     movf      STATUS_Save,w       ; Restore context
     movwf     STATUS
     swapf     W_Save,f            ; swapf doesn't affect Status bits, but MOVF would
     swapf     W_Save,w
     retfie
     
     
Start:
     bsf       STATUS,RP0          ; select Register Bank 1
     movlw     0xFF
     movwf     TRISA               ; Make PortA all input
     movlw     0x01
     movwf     TRISB               ; Make RB0 pin input (switch)
     clrf      TRISD               ; Make PortD all output

     movlw     0x00                ; Left Justified, Vdd-Vss referenced
     movwf     ADCON1

     movlw     B'0000011'         ; configure Prescaler on Timer0, max prescale (/256)
     movwf     OPTION_REG          ; configure

     bsf       STATUS,RP1          ; select Register Bank 3
     movlw     0xFF                ; we want all Port A pins Analog
     movwf     ANSEL
     movlw     0x00
     movwf     ANSELH              ; PortB pins are digitial (important as RB0 is switch)
     bcf       STATUS,RP0          ; back to Register Bank 0
     bcf       STATUS,RP1
     
     movlw     0x41
     movwf     ADCON0         ; configure A2D for Fosc/8, Channel 0 (RA0), and turn on the A2D module
     movlw     0x08
     movwf     Display
     clrf      Direction
;	movlw	.
     clrf      LookingFor          ; Looking for a 0 on the button
     
     
     movlw     B'10100000'         ; enable Timer 0 and global interrupts
     movwf     INTCON
;	Call SetDelay
MainLoop:
	banksel Delay1

	Skip 
	banksel ADCON0
	nop
	nop
	nop
	nop
	nop  
	CheckA2D:
		bsf       ADCON0,GO_DONE      ; start conversion
		btfss     ADCON0,GO_DONE      ; this bit will change to zero when the conversion is complete
		goto      $-1
		movfw ADRESH;w now has the bit value of potentiometer
		movwf Display;This command show that through LEDs
	
	movf      Display,w           ; Copy the display to the LEDs
	andlw		b'11111110'			;last bit is for task 2
	movwf	Display
	btfsc	Display2,0;turn on bit 0 of display
		bsf	Display,0


	movfw	Display
	movwf     PORTD
     




EndMainLoop:
     movlw     .13
    ; call      Delay               ; delay ~10mS (13 * 775uS)
     goto      Start

; Delay Subroutine.  Enter delays Wreg * 771uS + 5 uS including call and return
SetDelay:
	banksel Delay1
	movlw 	b'00000011'
	movwf	Delay1
return

     end
     
